package com.example.roadeyeserver.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.roadeyeserver.constant.ProjectConstant;
import com.example.roadeyeserver.entity.StationRoadAreaInfo;
import com.example.roadeyeserver.entity.TobjTollStationInfo;
import com.example.roadeyeserver.entity.request.StationRoadAreaInfoRequest;
import com.example.roadeyeserver.entity.vo.StationRoadAreaInfoVO;
import com.example.roadeyeserver.mapper.StationRoadAreaInfoMapper;
import com.example.roadeyeserver.result.Result;
import com.example.roadeyeserver.service.StationRoadAreaInfoService;
import com.example.roadeyeserver.service.TobjTollStationInfoService;
import com.example.roadeyeserver.util.ResultUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.annotation.PostConstruct;
import javax.annotation.Resource;
import java.util.Comparator;
import java.util.List;
import java.util.Map;
import java.util.function.Function;
import java.util.stream.Collectors;

@Service
@Slf4j
public class StationRoadAreaInfoServiceImpl extends ServiceImpl<StationRoadAreaInfoMapper, StationRoadAreaInfo> implements StationRoadAreaInfoService {

    @Resource
    private StationRoadAreaInfoMapper stationRoadAreaInfoMapper;

    @Resource
    private TobjTollStationInfoService tobjTollStationInfoService;

    @Override
    public Result<List<StationRoadAreaInfoVO>> getAllRoad() {
        // 查询所有信息，并根据路段编码进行分组
        List<StationRoadAreaInfoVO> roadInfos = stationRoadAreaInfoMapper.getAllRoad();
        return ResultUtil.success(roadInfos);
    }

    @Override
    public Result<List<StationRoadAreaInfoVO>> getAllArea() {
        // 查询所有信息，并根据区域进行分组
        List<StationRoadAreaInfoVO> areaInfos = stationRoadAreaInfoMapper.getAllArea();
        return ResultUtil.success(areaInfos);
    }

    @Override
    public Result<List<StationRoadAreaInfoVO>> getStationInStartEnd(StationRoadAreaInfoRequest request) {
        // 开始收费站id
        String startStationId = request.getStartStationId();
        // 结束收费站id
        String endStationId = request.getEndStationId();
        if (StringUtils.isEmpty(startStationId) || StringUtils.isEmpty(endStationId)) {
            return ResultUtil.fail("开始或结束收费站id不能为空!");
        }
        // 查询开始收费站
        StationRoadAreaInfo startStation = this.getOne(new LambdaQueryWrapper<StationRoadAreaInfo>()
                .eq(StationRoadAreaInfo::getStationId, startStationId)
        );
        // 查询结束收费站
        StationRoadAreaInfo endStation = this.getOne(new LambdaQueryWrapper<StationRoadAreaInfo>()
                .eq(StationRoadAreaInfo::getStationId, endStationId)
        );
        if (startStation == null || endStation == null) {
            return ResultUtil.fail("开始或结束收费站不存在!");
        }
        Integer pileNumValueStart = startStation.getPileNumValue();
        Integer pileNumValueEnd = endStation.getPileNumValue();
        // 较大的桩号
        Integer pileNumValueMax = Math.max(pileNumValueStart, pileNumValueEnd);
        // 较小的桩号
        Integer pileNumValueMin = Math.min(pileNumValueStart, pileNumValueEnd);
        String roadCodeStart = startStation.getRoadCode();
        String roadCodeEnd = endStation.getRoadCode();
        if (!StringUtils.equals(roadCodeStart, roadCodeEnd)) {
            return ResultUtil.fail("开始收费站和结束收费站必须在同一路段上!");
        }
        // 查询同一路段上在始收费站和结束收费站之间的所有收费站
        List<StationRoadAreaInfo> stationRoadAreaInfos = this.list(new LambdaQueryWrapper<StationRoadAreaInfo>()
                .eq(StationRoadAreaInfo::getRoadCode, roadCodeStart)
                .ge(StationRoadAreaInfo::getPileNumValue, pileNumValueMin)
                .le(StationRoadAreaInfo::getPileNumValue, pileNumValueMax)
        );
        // 查询同一路段的收费站经纬度信息
        List<TobjTollStationInfo> tollStationInfos = tobjTollStationInfoService.list(new LambdaQueryWrapper<TobjTollStationInfo>()
                .eq(TobjTollStationInfo::getRoad, roadCodeStart)
        );
        // 构建stationId-TobjTollStationInfo映射
        Map<String, TobjTollStationInfo> tollStationInfoMap = tollStationInfos.stream()
                .collect(Collectors.toMap(TobjTollStationInfo::getStationId, Function.identity()));
        List<StationRoadAreaInfoVO> result = stationRoadAreaInfos.stream().map(stationRoadAreaInfo -> {
            StationRoadAreaInfoVO stationRoadAreaInfoVO = BeanUtil.copyProperties(stationRoadAreaInfo, StationRoadAreaInfoVO.class);
            // 收费站id
            String stationId = stationRoadAreaInfo.getStationId();
            TobjTollStationInfo tollStationInfo = tollStationInfoMap.get(stationId);
            if (tollStationInfo != null) {
                stationRoadAreaInfoVO.setLon(tollStationInfo.getLng());
                stationRoadAreaInfoVO.setLat(tollStationInfo.getLat());
            }
            return stationRoadAreaInfoVO;
        })
                .sorted(Comparator.comparingInt(StationRoadAreaInfoVO::getPileNumValue))
                .collect(Collectors.toList());
        // 按桩号从小到大排序
        return ResultUtil.success(result);
    }

    @Override
    public String getRoadCodeByStationId(String stationId) {
        StationRoadAreaInfo stationRoadAreaInfo = this.getOne(new LambdaQueryWrapper<StationRoadAreaInfo>()
                .eq(StationRoadAreaInfo::getStationId, stationId)
        );
        if (stationRoadAreaInfo != null) {
            return stationRoadAreaInfo.getRoadCode();
        }
        return "";
    }

    //    @PostConstruct
    public void createTable() {
        log.info("开始创建station_road_area_info");
        stationRoadAreaInfoMapper.createTable();
        log.info("station_road_area_info创建完成");
        List<StationRoadAreaInfo> list = this.list();
        log.info("station_road_area_info数据量:{},数据:{}", list.size(), list);
    }

}
